/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import Model.Customer;
import Model.HibernateUtil;
import Model.TransactionId;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author user
 */
public class ProcessConfirm extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String toAccount = request.getParameter("toaccount");
        int amount = 0;
        try {
            amount = Integer.parseInt(request.getParameter("amount"));
        } catch (Exception e) {
            request.setAttribute("notnumber", "Số tiền quý khách nhập không hợp lệ");

        }
        String content = request.getParameter("content");
        System.out.println(content);
        if (content.equals("")) {
            request.setAttribute("content", "Quý khách chưa nhập nội dung chuyển khoản");
        }
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Query q = session.createQuery("from Customer cus where cus.accountid='" + toAccount + "'");
        Customer toCus = (Customer) q.uniqueResult();
        if (toCus == null) {
            request.setAttribute("notexist", "Tài khoản quý khách nhập không tồn tại");
        }
        if (toCus == null | amount == 0 || content == "") {
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/transfer.jsp");
            dispatcher.forward(request, response);
        } else {
            HttpSession sessionTransfer = request.getSession();
            Customer fromCus = (Customer) sessionTransfer.getAttribute("sessionlogin");
            sessionTransfer.setAttribute("transfer", toCus);
            Model.Transaction tran = new Model.Transaction(new Model.TransactionId(toCus.getId(), new Date()), 1, amount, content, fromCus.getAccountid(), toCus.getAccountid(), fromCus.getName(), toCus.getName());
            sessionTransfer.setAttribute("transaction", tran);
            response.sendRedirect("confirm.jsp");

        }


    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
